package com.suisung.shopsuite.common.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 *
 * @author Xinze
 * @since 2018-02-22 11:29:05
 */
//@EnableOpenApi
//@Configuration
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

    @Bean
    public Docket baseRestApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("基础版")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.suisung.shopsuite"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("小程序接口文档")
                .description("小程序接口文档Swagger版")
                .termsOfServiceUrl("")
                .contact(new Contact("数探南京","https://www.seek-nj.com/", ""))
                .version("1.0")
                .build();
    }

//    @Resource
//    private ConfigProperties config;
//
//    @Bean
//    public Docket createRestApi() {
//        Docket docket = new Docket(DocumentationType.OAS_30);
//        if (StrUtil.isNotBlank(config.getSwaggerHost())) {
//            docket.host(config.getSwaggerHost());
//        }
//        return docket
//                .apiInfo(apiInfo())
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(config.getSwaggerBasePackage()))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//    }
//
//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title(config.getSwaggerTitle())
//                .description(config.getSwaggerDescription())
//                .version(config.getSwaggerVersion())
//                .termsOfServiceUrl("")
//                .build();
//    }
//
//    private List<SecurityScheme> securitySchemes() {
//        return Collections.singletonList(
//                new ApiKey("Authorization", "Authorization", "header")
//        );
//    }
//
//    private List<SecurityContext> securityContexts() {
//        AuthorizationScope[] scopes = {new AuthorizationScope("global", "accessEverything")};
//        List<SecurityReference> references = Collections.singletonList(
//                new SecurityReference("Authorization", scopes)
//        );
//        return Collections.singletonList(SecurityContext.builder()
//                .securityReferences(references)
//                .build());
//    }

}
